<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{ article.comm_count }})</span>
        <span>0 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getComments"
    >
      <div class="list">
        <div
          class="item van-hairline--bottom"
          v-for="item in commentList"
          :key="item.com_id"
        >
          <van-image round width="10vw" height="10vw" :src="item.aut_photo" />
          <div class="info">
            <p>
              <span class="name">{{ item.aut_name }}</span>
              <span class="zan">
                {{ item.like_count }}
                <geek-icon name="like2" />
              </span>
            </p>
            <p class="cont">{{ item.content }}</p>
            <p>
              <span class="reply" @click="reply(item)">
                {{ item.reply_count }}回复
                <i class="van-icon van-icon-arrow"></i>
              </span>
              <span class="time">{{ item.pubdate | formatTime }}</span>
            </p>
          </div>
        </div>
      </div>
    </van-list>
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input" @click="sendComment">
        <i class="van-icon van-icon-edit"></i>
      </div>
      <div class="btn" @click="$emit('commentScroll', true)">
        <geek-icon name="comment"></geek-icon>
        <p>评论</p>
        <i>{{ article.comm_count }}</i>
      </div>
      <div class="btn" @click="liking">
        <geek-icon
          :name="article.attitude === 1 ? 'like-sel' : 'like'"
        ></geek-icon>
        <p>点赞</p>
      </div>
      <div class="btn" @click="collection">
        <geek-icon
          :name="article.is_collected ? 'collect-sel' : 'collect'"
        ></geek-icon>
        <p>收藏</p>
      </div>
      <div class="btn">
        <geek-icon name="share"></geek-icon>
        <p>分享</p>
      </div>
    </div>
    <!-- 评论回复组件 -->
    <ReplyComment
      v-model="isShow"
      :comment="comment"
      :commentList="commentList"
      :article="article"
      ref="replyComment"
    ></ReplyComment>
  </div>
</template>

<script>
import {
  commentsGet,
  articleLikings,
  articleCollections
} from '@/api/article.js'
import ReplyComment from './reply-comment.vue'
export default {
  name: 'ArticleComment',
  components: {
    ReplyComment
  },
  props: ['article'],
  data () {
    return {
      offset: null,
      commentList: [],
      loading: false,
      finished: false,
      isShow: false,
      comment: ''
    }
  },
  methods: {
    // 获取文章评论
    async getComments () {
      const res = await commentsGet({
        type: 'a',
        source: this.$route.params.articleId,
        offset: this.offset
      })
      this.loading = false
      this.commentList.push(...res.data.results)
      if (res.data.end_id === res.data.last_id) {
        this.finished = true
      } else {
        this.offset = res.data.last_id
      }
    },
    // 文章点赞和取消点赞
    async liking () {
      await articleLikings(
        this.$route.params.articleId,
        this.article.attitude === -1
      )
      this.article.attitude = this.article.attitude * -1
      this.$toast.success(
        this.article.attitude === 1 ? '点赞成功' : '取消点赞成功'
      )
    },
    // 文章收藏和取消收藏
    async collection () {
      await articleCollections(
        this.$route.params.articleId,
        this.article.is_collected === false
      )
      this.article.is_collected = !this.article.is_collected
      this.$toast.success(
        this.article.is_collected ? '收藏成功' : '取消收藏成功'
      )
    },
    // 回复按钮点击事件
    reply (item) {
      this.isShow = true
      this.comment = item
    },
    // 打开发表文章评论弹窗
    sendComment () {
      this.$emit('commentScroll', false)
      this.$refs.replyComment.inputShow = true
      this.comment = ''
    }
  }
}
</script>

<style lang="less" scoped>
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;

    span {
      font-size: 16px;

      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }

  .list {
    padding: 0 16px;

    .item {
      display: flex;
      padding: 10px 0;

      .info {
        padding-left: 10px;
        flex: 1;

        p {
          margin: 0;

          .name {
            font-size: 16px;
          }

          .zan {
            font-size: 14px;
            float: right;
            color: #999;

            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }

          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }

          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;

            .van-icon {
              position: relative;
              top: 1px;
            }
          }

          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }

  ::v-deep .van-list__finished-text {
    padding: 0 0 40px;
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 50px;
    background: #fff;
    display: flex;
    width: 100%;
    align-items: center;

    .input {
      margin-left: 10px;
      width: 200px;
      height: 34px;
      background: @geek-gray-color;
      border-radius: 17px;
      line-height: 36px;
      padding-left: 10px;

      .van-icon {
        color: #999;
      }
    }

    .btn {
      flex: 1;
      text-align: center;
      position: relative;

      p {
        margin: 0;
        font-size: 10px;
      }

      .geek-icon {
        font-size: 18px;
      }

      i {
        height: 16px;
        min-width: 16px;
        padding: 0 3px;
        background: @geek-color;
        color: #fff;
        font-size: 10px;
        position: absolute;
        right: 0;
        top: -4px;
        line-height: 16px;
        border-radius: 8px;
        font-style: normal;
      }
    }
  }
}
</style>
